package com.db.dbcloud;

import com.db.utils.ButtonStyleUtil;
import com.db.utils.ErrorJSONUtil;
import com.db.utils.FinalHttpUtil;
import com.db.utils.GsonUtil;
import com.db.utils.ServiceUtil;
import com.db.vo.User;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

public class RegisterActivity extends FinalActivity{
	@ViewInject(id = R.id.register_return_btn, click = "clickReturn")private ImageButton returnBtn;
	@ViewInject(id = R.id.register_register_btn, click = "clickRegister")private ImageButton registerBtn;
	@ViewInject(id = R.id.register_user)private EditText userText;
	@ViewInject(id = R.id.register_password)private EditText passText;
	@ViewInject(id = R.id.register_password2)private EditText passText_2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_register);
		ButtonStyleUtil.setButtonFocusChanged(returnBtn);
		ButtonStyleUtil.setButtonFocusChanged(registerBtn);
		userText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(s.length() > 0){
					int pos = s.length() - 1;
					if(s.length() > 16){
						((Editable) s).delete(pos, pos + 1);
					}
				}
				if(s.length() <= 5){
					registerBtn.setEnabled(false);
				}else if(s.length() >= 6 && passText.getText().length() >= 6 ){
					registerBtn.setEnabled(true);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
		passText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(s.length() > 0){
					int pos = s.length() - 1;
					if(s.length() > 16){
						((Editable) s).delete(pos, pos + 1);
					}
				}
				if(s.length() <= 5){
					registerBtn.setEnabled(false);
				}else if(s.length() >= 6 && userText.getText().length() >= 6){
					registerBtn.setEnabled(true);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
	}
	public void clickReturn(View v){
		RegisterActivity.this.finish();
	}
	public void clickRegister(View v){
		String pass = passText.getText().toString();
		String pass2 = passText_2.getText().toString();
		if(pass.equals(pass2)){
			ajaxRegister();
		}else{
			Toast.makeText(RegisterActivity.this, "两次密码输入不一致", Toast.LENGTH_LONG).show();
		}
	}
	private void ajaxRegister(){
		FinalHttp finalHttp = FinalHttpUtil.getFinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("uid", userText.getText().toString());
		params.put("password", passText.getText().toString());
		String url = ServiceUtil.domainUrl + "/register";
		finalHttp.configCharset("utf-8");
		finalHttp.post(url, params, new AjaxCallBack<String>() {
			ProgressDialog pd = null;
			@Override
			public void onStart() {
				super.onStart();
				pd = new ProgressDialog(RegisterActivity.this);
				pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
	    		pd.setCanceledOnTouchOutside(false);
	    		pd.setTitle("注册");
	    		pd.setMessage("正在注册...");
	    		pd.show();
			}
			@Override
			public void onFailure(Throwable t, String strMsg) {
				super.onFailure(t, strMsg);
				Toast.makeText(RegisterActivity.this, "注册失败，请检查网络问题", Toast.LENGTH_LONG).show();
				pd.hide();
			}
			@Override
			public void onSuccess(String t) {
				super.onSuccess(t);
				if(ErrorJSONUtil.isError(t)){
					Toast.makeText(RegisterActivity.this, ErrorJSONUtil.getErrorMsg(t), Toast.LENGTH_LONG).show();
				}else{
					User user = GsonUtil.getGson().fromJson(t, User.class);
					Intent intent = RegisterActivity.this.getIntent();
					intent.putExtra("retuser", user.getuId());
					intent.putExtra("retpass", user.getuPassword());
					RegisterActivity.this.setResult(RESULT_OK, intent);
					RegisterActivity.this.finish();
				}
			}
		});
	}
}
